package com.ff.xyh.service.post.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.post.PostReply;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.vo.post.PostReplyBaseInfo;
import com.ff.xyh.common.vo.post.PostReplyVo;
import com.ff.xyh.common.qo.post.PostReplyQuery;
import com.ff.xyh.service.post.service.PostReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帖子回复表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/post-service")
public class PostReplyController {

    @Autowired
    PostReplyService postReplyService;

    /**
     * 条件查询帖子回复
     * @param postReplyQuery
     * @return
     */
    @PostMapping("get-reply-condition")
    public ResultObject getPostReplyCondition(
            @RequestBody PostReplyQuery postReplyQuery) {
        return postReplyService.getPostReplyCondition(postReplyQuery);
    }

    /**
     * 获取帖子的所有回复
     * @param postId
     * @param postType
     * @return
     */
    @PostMapping("get-reply-post-id-type")
    public ResultObject getPostReplyByPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {

        QueryWrapper<PostReply> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .lt("complaintNumber", 10);

        List<PostReply> list = postReplyService.list(wrapper);
        return ResultObject.ok().data("items", list);
    }

    /**
     * 获取并封装帖子的所有回复
     * @param postId
     * @param postType
     * @return
     */
    @PostMapping("get-all-replyVo-post-id-type")
    public ResultObject getAllReplyVoByPostIdAndType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {
        List<PostReplyVo> list =
                postReplyService.getPostReplieVoListByPostIdAndPostType(postId, postType);
        if (list == null)
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        else
            return ResultObject.ok().data("items", list);
    }

    /**
     * 获取帖子的一级回复
     * @param postId
     * @param postType
     * @return
     */
    @PostMapping("get-first-replies-post-id-type")
    public ResultObject getFirstPostRepliesByPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {
        List<PostReply> firstPostReplyList = postReplyService.getFirstPostRepliesByPostIdAndPostType(postId, postType);

        List<PostReplyBaseInfo> list = new ArrayList<>(firstPostReplyList.size());
        for (PostReply postReply : firstPostReplyList) {
            PostReplyBaseInfo postReplyBaseInfo = new PostReplyBaseInfo();
            BeanUtils.copyProperties(postReply, postReplyBaseInfo);
            list.add(postReplyBaseInfo);
        }
        if (list == null)
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        else
            return ResultObject.ok().putList(list, list.size());
    }


//    @GetMapping("get-child-reply-parent-id")
//    public ResultObject getChildPostReplyByParentId(
//            @RequestParam(name = "parentId") String parentId) {
//
//        QueryWrapper<PostReply> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id", parentId)
//                .lt("complaintNumber", 10);
//
//        List<PostReply> list = postReplyService.list(wrapper);
//        return ResultObject.ok().data("data", list);
//    }

//    /**
//     * 获取第一回复下的所有子对象id
//     * @param FirstId
//     * @return
//     */
//    @GetMapping("get-all-child-replies-first-id")
//    public ResultObject getAllChildPostRepliesByFirstId(
//            @RequestParam(name = "FirstId") String FirstId) {
//
//        List<PostReplyBaseInfo> list = postReplyService.getAllChildrenPostReplyByFirstReplyId(FirstId);
//        if (list == null)
//            return ResultObject.error().message("没有子回复信息");
//        else
//            return ResultObject.ok().data("items", list);
//    }

    /**
     * 添加回复
     * @param postReply
     * @return
     */
    @PostMapping("insert-reply")
    public ResultObject insertPostReply(
            @RequestBody PostReply postReply) {
        if (postReplyService.save(postReply)) {
            return ResultObject.ok().data("data", postReply);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }

    @DeleteMapping("delete-reply-id")
    public ResultObject deletePostReplyById(
            @RequestParam(name = "replyId") String replyId) {
        PostReply postReply = postReplyService.getById(replyId);
        if (postReply == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            if (postReplyService.removeById(replyId)) {
                return ResultObject.ok().putData(postReply);
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_DELETE);
            }
        }
    }







}

